package com.zzyl.common.utils.pdf;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;

import java.io.IOException;
import java.io.InputStream;

/**
 * PDF读取工具类
 *
 * @Author mr.wu
 * @Date 2025-5-20 11:16
 */
public class PdfUtils {
    /**
     * 基于PDF文件读取全部内容字符串
     * @param inputStream
     * @return
     */
    public static String pdfToString(InputStream inputStream) {
        PDDocument pdDocument = null;
        try {
            pdDocument = PDDocument.load(inputStream);

            PDFTextStripper pdfTextStripper = new PDFTextStripper();
            String pdfText = pdfTextStripper.getText(pdDocument);

            return pdfText;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(null!=pdDocument) {
                try {
                    pdDocument.close();
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
}
